package com.zrwl.poscloud.xo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.*;
import com.zrwl.poscloud.base.enums.EIBatchType;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.*;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.CompanyDictDataMapper;
import com.zrwl.poscloud.xo.mapper.CompanyMapper;
import com.zrwl.poscloud.xo.mapper.ImportDetailMapper;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 导入明细 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-11-02
 */
@Service
public class ImportDetailServiceImpl extends SuperServiceImpl<ImportDetailMapper, ImportDetail> implements ImportDetailService {

    @Autowired
    private ImportDetailService importDetailService;
    @Resource
    private ImportDetailMapper importDetailMapper;
    @Autowired
    private ImportBatchService importBatchService;
    @Autowired
    private EmployeeService employeeService;
    @Resource
    private CompanyDictDataMapper companyDictDataMapper;
    @Autowired
    private CompanyDictDataService companyDictDataService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private DeptAdminRService deptAdminRService;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private DeptService deptService;

    @Override
    public IPage<ImportDetail> getPageList(ImportDetailVO importDetailVO) {
        ImportBatch importBatch = importBatchService.getById(importDetailVO.getBatchUid());
        String column = null;
        Map<String, String> columnConfigMap = JSONObject.parseObject(importBatch.getColumnConfig(), LinkedHashMap.class, Feature.OrderedField);//按序排列
        for (Map.Entry<String, String> stringStringEntry : columnConfigMap.entrySet()) {
            if (stringStringEntry.getValue().contains("所属机构")) {
                column = stringStringEntry.getKey();
            }
        }
        QueryWrapper<ImportDetail> queryWrapper = new QueryWrapper<>();
        String[] filedName = ClassUtil.getFiledName(new ImportDetail());
        Page<ImportDetail> page = new Page<>();
        page.setCurrent(importDetailVO.getCurrentPage());
        page.setSize(importDetailVO.getPageSize());
        if (StringUtils.isNotEmpty(importDetailVO.getBatchUid()) && StringUtils.isNotEmpty(importDetailVO.getBatchUid().trim())) {
            queryWrapper.eq("batch_uid", importDetailVO.getBatchUid());
        }
        if (StringUtils.isNotEmpty(column) && StringUtils.isNotEmpty(column.trim()) && importBatch.getType() != 3) {
            queryWrapper.in(column, importDetailVO.getDeptRoleTree());
        }

        if (importDetailVO.getLineNumber() != 0) {
            queryWrapper.eq("line_number", importDetailVO.getLineNumber());
        }

        for (String s : filedName) {
            if (s.contains("column")) {
                if (StringUtils.isNotEmpty(importDetailVO.getKeyword()) && !StringUtils.isEmpty(importDetailVO.getKeyword().trim())) {
                    queryWrapper.like(s, importDetailVO.getKeyword().trim()).or()
                            .like("summary",importDetailVO.getKeyword().trim());
                }
            }
        }
        if (importDetailVO.getStatus() != 0) {
            queryWrapper.eq("status",importDetailVO.getStatus());
        }else {
            queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        }
        queryWrapper.orderByAsc("line_number");
        IPage<ImportDetail> pageList = importDetailService.page(page, queryWrapper);
        List<ImportDetail> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<ImportDetail> getAllList(ImportDetailVO importDetailVO) {
        QueryWrapper<ImportDetail> queryWrapper = new QueryWrapper<>();
        String[] filedName = ClassUtil.getFiledName(new ImportDetail());
        if (StringUtils.isNotEmpty(importDetailVO.getBatchUid()) && StringUtils.isNotEmpty(importDetailVO.getBatchUid().trim())) {
            queryWrapper.eq("batch_uid", importDetailVO.getBatchUid());
        }
        for (String s : filedName) {
            if (s.contains("column")) {
                if (StringUtils.isNotEmpty(ClassUtil.getFieldValueByFieldName(s, importDetailVO)) && !StringUtils.isEmpty(ClassUtil.getFieldValueByFieldName(s, importDetailVO).trim())) {
                    queryWrapper.eq(s, ClassUtil.getFieldValueByFieldName(s, importDetailVO));
                }
            }
        }
        if (importDetailVO.getLineNumber() != 0) {
            queryWrapper.eq("line_number", importDetailVO.getLineNumber());
        }
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<ImportDetail> list = importDetailService.list(queryWrapper);
        return list;
    }

    @Override
    public String add(ImportDetailVO importDetailVO) {
        int count = 0;
        if (importDetailVO.getBatchUid() != null) {
            ImportBatch importBatch = importBatchService.getById(importDetailVO.getBatchUid());
            ImportBatchVO importBatchVO = new ImportBatchVO();
            BeanUtils.copyProperties(importBatch, importBatchVO);
            count = importBatchVO.getTotalCount() + 1;
            importBatchVO.setTotalCount(importBatchVO.getTotalCount() + 1);
            importBatchService.edit(importBatchVO);
        }
        if (importDetailVO.getLineNumber() == 0) {
            importDetailVO.setLineNumber(count);
        }
        ImportDetail entity = new ImportDetail();
        BeanUtils.copyProperties(importDetailVO, entity, SysConf.STATUS);
        entity.insert();
        importDetailVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(ImportDetailVO importDetailVO) {
        ImportDetail entity = importDetailService.getById(importDetailVO.getUid());
        BeanUtils.copyProperties(importDetailVO, entity);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(ImportDetailVO importDetailVO) {
        ImportDetail entity = importDetailService.getById(importDetailVO.getUid());
        if (entity.getBatchUid() != null) {
            ImportBatch importBatch = importBatchService.getById(entity.getBatchUid());
            ImportBatchVO importBatchVO = new ImportBatchVO();
            BeanUtils.copyProperties(importBatch, importBatchVO);
            importBatchVO.setTotalCount(importBatchVO.getTotalCount() - 1);
            if (entity.getStatus() == 2) {
                importBatchVO.setImportedCount(importBatch.getImportedCount() - 1);
            }
            importBatchService.edit(importBatchVO);
        }
        entity.setStatus(EStatus.DISABLED);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public List<ImportDetail> downloadTemplateData(ImportDetailVO importDetailVO) {
        ImportBatch importBatch = importBatchService.getById(importDetailVO.getBatchUid());
        Company company = companyService.getByCode(importDetailVO.getCompanyCode().substring(0, 5));
        return importDetailMapper.downloadTemplateData(DeptUtil.getLatestCompnay(importDetailVO.getCompanyCode()), importBatch.getTotalCount(), importBatch.getType(), company.getCompanyDictTypeUid(), company.getUid(), deptAdminRService.getMyDeptRoleTreeCode(), company.getShortName());
    }

//    /**
//     * 导入明细的有效性
//     *
//     * @param importDetailVO  导入明细
//     * @param importBatch     导入批次
//     * @param columnConfigMap 列配置
//     * @param parseObject
//     * @return
//     * @throws Exception
//     */
//    @Override
//    public void verifyImportDetailVO(ImportDetailVO importDetailVO, ImportBatch importBatch, Map<String, String> columnConfigMap, List<CompanyBusinessFeeConfig> parseObject) throws Exception {
//        String certCode = null;
//        double dailyLimit = 0;
//        String employeeCode = null;
//        EmployeeVO employeeVO = null;
//        String superCompanyName = null;
//        String companyName = null;
//        AdminVO adminVO = adminService.getMe();
//        Company company = companyService.getById(adminVO.getCompanyUid());
//        List<String> dictTypeList = new ArrayList<>();
//        dictTypeList.add("employee_pay_level");
//        Map<String, Map<String, Object>> listByCompanyDictTypeList = companyDictDataService.getListByCompanyDictTypeList(dictTypeList, company.getCompanyDictTypeUid(), adminVO.getCompanyUid());
//        for (Map.Entry<String, String> entry : columnConfigMap.entrySet()) {
//            if (importBatch.getType() < 5) {
//                if ("身份证号".equals(entry.getValue().split(",")[0]) && StringUtils.isNotEmpty(ClassUtil.getFieldValueByFieldName(entry.getKey(), importDetailVO)) && StringUtils.isNotEmpty(ClassUtil.getFieldValueByFieldName(entry.getKey(), importDetailVO).trim())) {
//                    certCode = ClassUtil.getFieldValueByFieldName(entry.getKey(), importDetailVO);
//                }
//                if ("日限额".equals(entry.getValue().split(",")[0]) && StringUtils.isNotEmpty(ClassUtil.getFieldValueByFieldName(entry.getKey(), importDetailVO)) && StringUtils.isNotEmpty(ClassUtil.getFieldValueByFieldName(entry.getKey(), importDetailVO).trim())) {
//                    dailyLimit = Double.parseDouble(ClassUtil.getFieldValueByFieldName(entry.getKey(), importDetailVO));
//                }
//                if (StringUtils.isNotEmpty(certCode) && StringUtils.isNotEmpty(certCode.trim()) && dailyLimit != 0) {
//                    break;
//                }
//            }
//            if (importBatch.getType() >= 5) {
//                if ("上级机构".equals(entry.getKey().split("\\(")[0]) && StringUtils.isNotEmpty(entry.getValue()) && StringUtils.isNotEmpty(entry.getValue().trim())) {
//                    superCompanyName = entry.getValue();
//                }
//                if ("所属机构".equals(entry.getKey().split("\\(")[0]) && StringUtils.isNotEmpty(entry.getValue()) && StringUtils.isNotEmpty(entry.getValue().trim())) {
//                    companyName = entry.getValue();
//                }
//                if ("人员编号".equals(entry.getKey().split("\\(")[0]) && StringUtils.isNotEmpty(entry.getValue()) && StringUtils.isNotEmpty(entry.getValue().trim())) {
//                    employeeCode = entry.getValue();
//                }
//                if (StringUtils.isNotEmpty(employeeCode) && StringUtils.isNotEmpty(employeeCode.trim()) && StringUtils.isNotEmpty(superCompanyName) && StringUtils.isNotEmpty(superCompanyName.trim()) && StringUtils.isNotEmpty(companyName) && StringUtils.isNotEmpty(companyName.trim())) {
//                    String companyCode = adminVO.getCompanyCode();
//                    if (superCompanyName.split("/").length > 1) {
//                        for (int l = 0; l < superCompanyName.split("/").length; l++) {
//                            if (l == 0) {
//                                companyCode = companyMapper.superCompanyCode(superCompanyName.split("/")[l]);
//                            } else {
//                                DeptVO dept = new DeptVO();
//                                dept.setName(superCompanyName.split("/")[l]);
//                                DeptVO deptVO1 = deptService.deptIsItEffective(dept, companyCode, false, false, true);//查询上级机构
//                                companyCode = deptVO1.getCompanyCode();
//                            }
//                        }
//                    }
//                    DeptVO dept = new DeptVO();
//                    dept.setName(companyName);
//                    employeeVO = new EmployeeVO();
//                    DeptVO deptVO = deptService.deptIsItEffective(dept, companyCode, false, false, true);//查询所属机构
//                    employeeVO.setEmployeeCode(employeeCode);
//                    employeeVO = employeeService.employeeIsItEffective(employeeVO, deptVO.getCompanyCode(), false, false, false, true, adminVO);
//                    if (employeeVO == null || StringUtils.isEmpty(employeeVO.getUid()) || StringUtils.isEmpty(employeeVO.getUid().trim())) {
//                        throw new Exception(importDetailVO.getLineNumber() + "行的持卡人不存在");
//                    }
//                    Employee employee = employeeService.getById(employeeVO.getUid());
//                    BeanUtils.copyProperties(employee, employeeVO);
//                    break;
//                }
//            }
//        }
//        for (Map.Entry<String, String> entry : columnConfigMap.entrySet()) {
//            String fieldValue = "";
//            fieldValue = ClassUtil.getFieldValueByFieldName(entry.getKey(), importDetailVO);
//            if (entry.getValue().split(",").length > 1) {
//                String condition = entry.getValue().split(",")[1];
//                //数据校验
//                for (String s : condition.split("\\|")) {
//                    switch (s) {
//                        case "非空":
//                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
//                                throw new Exception(entry.getValue().split(",")[0] + "不可为空");
//                            }
//                            break;
//                        case "去重":
//                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
//                                ImportDetailVO importDetail = new ImportDetailVO();
//                                ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetail, fieldValue);
//                                importDetail.setBatchUid(importDetailVO.getBatchUid());
//                                List<ImportDetail> allList = importDetailService.getAllList(importDetail);
//                                if (!allList.isEmpty()) {
//                                    if (importDetailVO.getUid() != null) {
//                                        if (allList.size() == 1) {
//                                            if (!importDetailVO.getUid().equals(allList.get(0).getUid())) {
//                                                throw new Exception(entry.getValue().split(",")[0] + "重复-数据库重复");
//                                            }
//                                        } else {
//                                            throw new Exception(entry.getValue().split(",")[0] + "重复-多条重复数据");
//                                        }
//                                    } else {
//                                        throw new Exception(entry.getValue().split(",")[0] + "重复-数据库重复");
//                                    }
//                                }
//                                if ("人员编号".equals(entry.getValue().split(",")[0])) {
//                                    employeeCode = ClassUtil.getFieldValueByFieldName(entry.getKey(), importDetailVO);
//                                }
//                                if ("身份证号".equals(entry.getValue().split(",")[0])) {
//                                    String upperCase = fieldValue.toUpperCase();
//                                    ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, upperCase);
//                                }
//                            }
//                            break;
//                        case "校验":
//                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
//                                if ("身份证号".equals(entry.getValue().split(",")[0]) && EmployeeUtil.isNotIdCard(fieldValue)) {
//                                    throw new Exception(entry.getValue().split(",")[0] + "不合法");
//                                }
//                                if (StringUtils.isNotEmpty(certCode) && StringUtils.isNotEmpty(certCode.trim())) {
//                                    if ("性别".equals(entry.getValue().split(",")[0]) && !EmployeeUtil.genderIsItEffective(fieldValue, certCode))
//                                        throw new Exception(entry.getValue().split(",")[0] + "与身份证号的性别不匹配");
//                                    if ("生日".equals(entry.getValue().split(",")[0]) && !EmployeeUtil.birthdayIsItEffective(fieldValue, certCode))
//                                        throw new Exception(entry.getValue().split(",")[0] + "与身份证号的出生日期不匹配");
//                                }
//                            }
//                            break;
//                        case "默认身份证号获取":
//                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
//                                if (StringUtils.isNotEmpty(certCode) && StringUtils.isNotEmpty(certCode.trim())) {
//                                    if ("人员编号".equals(entry.getValue().split(",")[0])) {
//                                        ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, certCode);
//                                        if (StringUtils.isEmpty(employeeCode) || StringUtils.isEmpty(employeeCode.trim())) {
//                                            employeeCode = certCode;
//                                        }
//                                    }
//                                    if ("性别".equals(entry.getValue().split(",")[0]))
//                                        ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, EmployeeUtil.genderBasedOnCertCode(certCode));
//                                    if ("生日".equals(entry.getValue().split(",")[0]))
//                                        ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, EmployeeUtil.birthdayBasedOnCertCode(certCode));
//                                }
//                            }
//                            break;
//                        case "系统自动生成":
//                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
//                                if ("人员编号".equals(entry.getValue().split(",")[0])) {
//                                    String autoEmployeeCode = employeeService.getAutoEmployeeCode();
//                                    if (StringUtils.isEmpty(employeeCode) || StringUtils.isEmpty(employeeCode.trim())) {
//                                        employeeCode = autoEmployeeCode;
//                                    }
//                                    ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, autoEmployeeCode);
//                                }
//                            }
//                            break;
//                        case "默认关":
//                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
//                                ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, "关");
//                            } else {
//                                if (!"关".equals(fieldValue) && !"开".equals(fieldValue))
//                                    throw new Exception(entry.getValue().split(",")[0] + "不合法");
//                            }
//                            break;
//                        case "默认等级":
//                            if ("消费等级".equals(entry.getValue().split(",")[0])) {
//                                List<CompanyDictData> companyDictDataList = (List<CompanyDictData>) listByCompanyDictTypeList.get("employee_pay_level").get("list");
//                                if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
//                                    for (CompanyDictData companyDictData : companyDictDataList) {
//                                        if ("默认等级".equals(companyDictData.getDictLabel())) {
//                                            ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, companyDictData.getDictLabel());
//                                            break;
//                                        }
//                                    }
//                                } else {
//                                    int pay = 0;
//                                    for (CompanyDictData companyDictData : companyDictDataList) {
//                                        if (companyDictData.getDictLabel().equals(fieldValue)) pay++;
//                                    }
//                                    if (pay == 0) throw new Exception(entry.getValue().split(",")[0] + "不合法");
//                                }
//                            }
//                            break;
//                        case "日期格式":
//                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
//                                try {
//                                    ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, DateUtils.formateDate(DateUtils.parseDate(fieldValue), "yyyy-MM-dd"));
//                                } catch (Exception e) {
//                                    throw new Exception(entry.getValue().split(",")[0] + "日期格式错误");
//                                }
//                            }
//                            break;
//                        case "时间格式":
//                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
//                                try {
//                                    ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, DateUtils.formateDate(DateUtils.parseDate(fieldValue), "yyyy-MM-dd HH:mm:ss"));
//                                } catch (Exception e) {
//                                    try {
//                                        ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, DateUtils.formateDate(DateUtils.parseDate(fieldValue), "yyyy-MM-dd 00:00:00"));
//                                    } catch (Exception ex) {
//                                        throw new Exception(entry.getValue().split(",")[0] + "时间格式错误");
//                                    }
//                                }
//                            }
//                            if ("账户有效时间".equals(entry.getValue().split(",")[0])) {
//
//                            }
//                            break;
//                        case "月限额<=日限额*30":
//                            if ("月限额".equals(entry.getValue().split(",")[0])) {
//                                if (dailyLimit != 0 && Double.parseDouble(fieldValue) > (dailyLimit * 30)) {
//                                    throw new Exception(entry.getValue().split(",")[0] + "超额");
//                                }
//                            }
//                            break;
//                        case "默认编号后六位":
//
//                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isNotEmpty(fieldValue.trim()) && StringUtils.isNotEmpty(employeeCode) && StringUtils.isNotEmpty(employeeCode.trim())) {
//                                String value = employeeCode.length() < 6 ? employeeCode : employeeCode.substring(employeeCode.length() - 6);
//                                if (value.length() < 6) {
//                                    String str = String.format("%06d", 0) + value;
//                                    String upperCase = str.toUpperCase();
//                                    value = upperCase.substring(upperCase.length() - 6);
//                                }
//                                ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, value);
//                            } else {
//                                if (fieldValue.length() < 6) {
//                                    String str = String.format("%06d", 0) + fieldValue;
//                                    String upperCase = str.toUpperCase();
//                                    ClassUtil.setFieldValueByFieldName(entry.getKey(), importDetailVO, upperCase.substring(upperCase.length() - 6));
//                                }
//                            }
//                            break;
//                    }
//                }
//            }
//        }
//    }

    @Override
    public boolean lineNumberVerify(String batchUid, int lineNumber) {
        QueryWrapper<ImportDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_uid", batchUid);
        queryWrapper.eq("line_number", lineNumber);
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        return importDetailService.count(queryWrapper) != 0;
    }

    @Override
    public ImportDetailVO singleImport(Map<String, String> columnConfigMap, List<CompanyBusinessFeeConfig> feeConfigList, List<CompanyDictData> payLevelList, List<SysDictData> cardTypeList, ImportBatch importBatch, ImportDetailVO importDetailVO, AdminVO adminVO, Map<String, Object> certCodeSuccessCertMap, Map<String, Object> employeeCodeSuccessCertMap, Map<String, DeptVO> deptRoleTreeEntity, Map<String, Object> deptSuccessCertMap) {
        //校验条件
        String certCode = null;//身份证号
        double dailyLimit = 0;//日限额
        String employeeCode = null;//人员编号
        Employee employee = null;//持卡人信息
        DeptVO deptVO = null;//机构信息
        for (Map.Entry<String, String> entry : columnConfigMap.entrySet()) {
            String key = entry.getValue().split(",")[0];//字段对应的名称
            String fieldName = entry.getKey();//字段名称
            String fieldValue = ClassUtil.getFieldValueByFieldName(fieldName, importDetailVO);
            //获取校验条件
            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                switch (EIBatchType.getByValue(importBatch.getType())) {
                    case employeeAccountOpen:
                        if ("身份证号".equals(key)) {
                            certCode = fieldValue;
                        }
                        if ("日限额".equals(key)) {
                            dailyLimit = Double.parseDouble(fieldValue);
                        }
                        if ("所属机构".equals(key)) {
                            deptVO = deptRoleTreeEntity.get(fieldValue);
                            if (deptVO == null) {
                                throw new RuntimeException(key + "不存在");
                            }
                        }
                        break;
                    case employeeAccountEdit:
                        if ("人员编号".equals(key)) {
                            employee = employeeService.getByCode(fieldValue);
                            employeeCode = employee.getEmployeeCode();
                        }
                        if ("身份证号".equals(key)) {
                            certCode = fieldValue;
                        }
                        if ("所属机构".equals(key)) {
                            deptVO = deptRoleTreeEntity.get(fieldValue);
                            if (deptVO == null) {
                                throw new RuntimeException(key + "不存在");
                            }
                        }
                        if ("日限额".equals(key)) {
                            dailyLimit = Double.parseDouble(fieldValue);
                        }
                        break;
                    case deptBatch:
                        if ("所属机构".equals(key)) {
                            deptVO = deptRoleTreeEntity.get(fieldValue);
                            if (deptVO == null) {
                                throw new RuntimeException(key + "不存在");
                            }

                            deptVO.setSuperCompanyCode(deptVO.getCompanyCode());
                        }
                        break;
                    case icCardOpen:
                    case icCardEdit:
                        if ("人员编号".equals(key)) {
                            employee = employeeService.getByCode(fieldValue);
                            employeeCode = employee.getEmployeeCode();
                        }
                        if ("所属机构".equals(key)) {
                            deptVO = deptRoleTreeEntity.get(key);
                        }
                        break;
                }
            }
        }
        for (Map.Entry<String, String> entry : columnConfigMap.entrySet()) {
            String key = entry.getValue().split(",")[0];//字段对应的名称
            String value = "";
            if(entry.getValue().split(",").length>1){
                value =  entry.getValue().split(",")[1];//字段校验信息
            }
            String fieldName = entry.getKey();//字段名称
            String fieldValue = ClassUtil.getFieldValueByFieldName(fieldName, importDetailVO);
            //进行校验
            try {
                if (importDetailVO.getStatus() != 2) {
                    for (String s : value.split("\\|")) {
                        switch (s) {
                            case "非空":
                                if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                    throw new RuntimeException(key + "不可为空");
                                }
                                if ("收费方式".equals(key)) {
                                    boolean contains = feeConfigList.contains(fieldValue);
                                    int count = 0;
                                    for (CompanyBusinessFeeConfig feeConfig : feeConfigList) {
                                        if (fieldValue.equals(feeConfig.getName())) {
                                            count++;
                                            break;
                                        }
                                    }
                                    if (count == 0) {
                                        throw new RuntimeException(key + "不合法");
                                    }
                                }
                                if ("消费等级".equals(key)) {
                                    int pay = 0;
                                    for (CompanyDictData dictData : payLevelList) {
                                        if (fieldValue.equals(dictData.getDictLabel())) {
                                            pay++;
                                        }

                                    }
                                    if (pay == 0)
                                        throw new RuntimeException(key + "不合法");
                                }
                                break;
                            case "去重":
                                if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                    ImportDetailVO importDetail = new ImportDetailVO();
                                    ClassUtil.setFieldValueByFieldName(fieldName, importDetail, fieldValue);
                                    importDetail.setBatchUid(importBatch.getUid());
                                    if(importDetailVO.getUid() == null){
                                        List<ImportDetail> allList = importDetailService.getAllList(importDetail);
                                        if (!allList.isEmpty()) {
                                            throw new RuntimeException(key + "重复-数据库重复");
                                        }
                                    }
                                    if ("人员编号".equals(key)) {
                                        if (employeeCodeSuccessCertMap.get(fieldValue) != null) {
                                            throw new RuntimeException(key + "重复-表内重复");
                                        } else {
                                            employeeCodeSuccessCertMap.put(fieldValue, importDetailVO);
                                        }
                                    }
                                    if ("导入新机构名称".equals(key)) {
                                        if (deptSuccessCertMap.get(fieldValue) != null) {
                                            throw new RuntimeException(key + "重复-表内重复");
                                        } else {
                                            deptSuccessCertMap.put(fieldValue, importDetailVO);
                                        }
                                    }
                                    if ("身份证号".equals(key)) {
                                        if (certCodeSuccessCertMap.get(fieldValue) != null) {
                                            throw new RuntimeException(key + "重复-表内重复");
                                        } else {
                                            certCodeSuccessCertMap.put(fieldValue, importDetailVO);
                                        }
                                    }
                                }
                                break;
                            case "校验":
                                if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                    if ("身份证号".equals(key) && EmployeeUtil.isNotIdCard(fieldValue)) {
                                        throw new RuntimeException(key + "不合法");
                                    }
                                    if (StringUtils.isNotEmpty(certCode) && StringUtils.isNotEmpty(certCode.trim())) {
                                        if ("性别".equals(key) && !EmployeeUtil.genderIsItEffective(fieldValue, certCode))
                                            throw new RuntimeException(key + "与身份证号的性别不匹配");
                                        if ("生日".equals(key) && !EmployeeUtil.birthdayIsItEffective(fieldValue, certCode))
                                            throw new RuntimeException(key + "与身份证号的出生日期不匹配");
                                    }
                                    if ("月限额".equals(key)) {
                                        if (Double.parseDouble(fieldValue) > (dailyLimit * 30) && dailyLimit != 0) {
                                            throw new RuntimeException(key + "超额");
                                        }
                                    }
                                }
                                break;
                            case "默认身份证号获取":
                                if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                    if (StringUtils.isNotEmpty(certCode) && StringUtils.isNotEmpty(certCode.trim())) {
                                        if ("人员编号".equals(key)) {
                                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                                fieldValue = certCode;
                                            }
                                            ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, fieldValue);
                                            employeeCode = fieldValue;
                                        }
                                        if ("性别".equals(key))
                                            ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, EmployeeUtil.genderBasedOnCertCode(certCode));
                                        if ("生日".equals(key))
                                            ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, EmployeeUtil.birthdayBasedOnCertCode(certCode));
                                    }
                                }
                                break;
                            case "系统自动生成":
                                if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                    if ("人员编号".equals(key)) {
                                        fieldValue = employeeService.getAutoEmployeeCode();
                                        if (StringUtils.isEmpty(employeeCode) || StringUtils.isEmpty(employeeCode.trim())) {
                                            employeeCode = fieldValue;
                                        }
                                        ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, fieldValue);
                                    }
                                }
                                break;
                            case "默认关":
                                if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                    ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, "关");
                                } else {
                                    if (!"关".equals(fieldValue) && !"开".equals(fieldValue))
                                        throw new RuntimeException(key + "不合法");
                                }
                                break;
                            case "消费卡":
                                if ("卡片类型".equals(key)) {
                                    int pay = 0;
                                    for (SysDictData dictData : cardTypeList) {
                                        if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                            if ("消费卡".equals(dictData.getDictLabel())) {
                                                pay++;
                                                ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, dictData.getDictLabel());
                                                break;
                                            }
                                        } else {
                                            if (fieldValue.equals(dictData.getDictLabel())) {
                                                pay++;
                                                break;
                                            }
                                        }
                                    }
                                    if (pay == 0)
                                        throw new RuntimeException(key + "不合法");
                                }
                                break;
                            case "日期格式":
                                if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                    try {
                                        ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, DateUtils.formateDate(DateUtils.parseDate(fieldValue), "yyyy-MM-dd"));
                                    } catch (Exception e) {
                                        throw new RuntimeException(key + "日期格式错误");
                                    }
                                }
                                break;
                            case "时间格式":
                                if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                    try {
                                        ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, DateUtils.formateDate(DateUtils.parseDate(fieldValue), "yyyy-MM-dd HH:mm:ss"));
                                    } catch (Exception e) {
                                        try {
                                            ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, DateUtils.formateDate(DateUtils.parseDate(fieldValue), "yyyy-MM-dd 00:00:00"));
                                        } catch (Exception ex) {
                                            throw new RuntimeException(key + "时间格式错误");
                                        }
                                    }
                                }
                                break;
                            case "默认编号后六位":
                                if (StringUtils.isEmpty(fieldValue) && StringUtils.isNotEmpty(employeeCode) && StringUtils.isNotEmpty(employeeCode.trim())) {
                                    fieldValue = employeeCode.length() < 6 ? employeeCode : employeeCode.substring(employeeCode.length() - 6);
                                    if (fieldValue.length() < 6) {
                                        String str = String.format("%06d", 0) + fieldValue;
                                        String upperCase = str.toUpperCase();
                                        fieldValue = upperCase.substring(upperCase.length() - 6);
                                    }
                                    ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, fieldValue);
                                } else {
                                    if (fieldValue.length() < 6) {
                                        String str = String.format("%06d", 0) + fieldValue;
                                        String upperCase = str.toUpperCase();
                                        fieldValue = upperCase.substring(upperCase.length() - 6);
                                    }
                                    ClassUtil.setFieldValueByFieldName(fieldName, importDetailVO, fieldValue);
                                }
                                break;
                        }
                    }
                }
            } catch (RuntimeException e) {
                importDetailVO.setSummary("失败原因 : " + e.getMessage());
                importDetailVO.setStatus(2);
            }
        }
        if (importDetailVO.getStatus() != 2) {
            if (importDetailVO.getUid() != null) {
                importDetailVO.setStatus(1);
                importDetailService.edit(importDetailVO);
            } else {
                importDetailService.add(importDetailVO);
            }
        }
        return importDetailVO;
    }
}